package com.itfreer.data.service.check;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;

import javax.ws.rs.DELETE;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.itfreer.data.check.entity.CheckConfigEntity;
import com.itfreer.data.check.service.CheckConfigService;
import com.itfreer.utils.json.JsonUtils;
/**
 * 检查配置rest服务
 * @author cai
 *
 */
@Component
@Path("/checkconfig/")
public class RestCheckConfigService {
	
	
	@Autowired
	private CheckConfigService checkConfigService;
	
	/**
	 * 解决请求参数问题
	 * 
	 * @param data
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static HashMap<String, Object> getRestParam(String data) {
		HashMap<String, Object> args = null;
		if (data == null || "".equals(data.trim()))
			return args;
		// 解决不同浏览器返回传递参数不一致问题-s-
		try {
			args = JsonUtils.toBean(data, HashMap.class);

		} catch (Exception e) {
			try {
				String ENCODE = "UTF-8";
				data = java.net.URLDecoder.decode(data, ENCODE);
				if (data != null && !"".equals(data.trim())) {
					String[] lst = data.split("&");
					args = new HashMap<String, Object>();
					for (String item : lst) {
						if (item == null || "".equals(item.trim()))
							continue;
						String[] ivalue = item.split("=");
						if (ivalue != null) {
							if (ivalue.length > 1) {
								args.put(ivalue[0], ivalue[1]);
							} else {
								args.put(ivalue[0], null);
							}
						}

					}
				}
			} catch (UnsupportedEncodingException e1) {
				e1.printStackTrace();
			}
		}
		// 解决不同浏览器返回传递参数不一致问题-e-
		return args;
	}

	/**
	 * 获取实体列表
	 * 
	 * @param queryFields
	 *            查询字段
	 * @param where
	 *            查询条件
	 * @param order
	 *            排序条件
	 * @param pageSize
	 *            分页大小
	 * @param pageIndex
	 *            分页索引
	 * @return
	 * @throws IOException
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@POST
	@Path("/getEntitys")
	@Produces(javax.ws.rs.core.MediaType.APPLICATION_JSON)
	public List<CheckConfigEntity> getEntitys(String data) throws IOException {
		HashMap<String, Object> args = getRestParam(data);
		String queryFields = null;
		String whereString = null;
		String orderString = null;
		Integer pageSize = 10;
		Integer pageIndex = 1;

		if (args != null) {
			if (args.containsKey("queryFields") && args.get("queryFields") != null)
				queryFields = args.get("queryFields").toString();
			if (args.containsKey("where") && args.get("where") != null)
				whereString = args.get("where").toString();
			if (args.containsKey("order") && args.get("order") != null)
				orderString = args.get("order").toString();
			if (args.containsKey("pageSize") && args.get("pageSize") != null)
				pageSize = new Integer(args.get("pageSize").toString());
			if (args.containsKey("pageIndex") && args.get("pageIndex") != null)
				pageIndex = new Integer(args.get("pageIndex").toString());
		}
		HashMap where = JsonUtils.toBean(whereString, HashMap.class);
		HashMap order = JsonUtils.toBean(orderString, HashMap.class);
		return checkConfigService.getEntitys(queryFields, where, order, pageSize, pageIndex);
	}

	/**
	 * 获取当前条件下的数据行总数
	 * 
	 * @param where
	 *            查询条件
	 * @return
	 * @throws IOException
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@POST
	@Path("/getRowCounts")
	@Produces(javax.ws.rs.core.MediaType.APPLICATION_JSON)
	public Long getRowCounts(String data) throws IOException {
		HashMap<String, Object> args = getRestParam(data);
		String whereString = null;
		if (args != null) {
			if (args.containsKey("where") && args.get("where") != null)
				whereString = args.get("where").toString();
		}
		HashMap where = JsonUtils.toBean(whereString, HashMap.class);
		return checkConfigService.getRowCounts(where);
	}

	/**
	 * 获取实体
	 * 
	 * @param guid
	 *            唯一值
	 * @return
	 */
	@POST
	@Path("/getEntity/{id:.+}")
	@Produces(javax.ws.rs.core.MediaType.APPLICATION_JSON)
	public CheckConfigEntity getEntity(@PathParam("id") String guid) {
		return checkConfigService.getEntity(guid);
	}

	/**
	 * 添加实体
	 * 
	 * @param entity
	 *            添加实体
	 * @return
	 * @throws IOException
	 */
	@POST
	@Path("/add")
	@Produces(javax.ws.rs.core.MediaType.APPLICATION_JSON)
	public CheckConfigEntity add(String data) throws IOException {
		HashMap<String, Object> args = getRestParam(data);
		String entityJson = null;
		if (args != null && args.containsKey("entity") && args.get("entity") != null) {
			entityJson = args.get("entity").toString();
		}
		CheckConfigEntity entity = (CheckConfigEntity) JsonUtils.toBean(entityJson, CheckConfigEntity.class);
		return checkConfigService.add(entity);
	}

	/**
	 * 更新实体
	 * 
	 * @param entity
	 *            实体
	 * @return
	 * @throws IOException
	 */
	@POST
	@Path("/update")
	@Produces(javax.ws.rs.core.MediaType.APPLICATION_JSON)
	public CheckConfigEntity update(String data) throws IOException {
		HashMap<String, Object> args = getRestParam(data);
		String entityJson = null;
		if (args != null && args.containsKey("entity") && args.get("entity") != null) {
			entityJson = args.get("entity").toString();
		}
		CheckConfigEntity entity = (CheckConfigEntity) JsonUtils.toBean(entityJson, CheckConfigEntity.class);
		return checkConfigService.update(entity);
	}

	/**
	 * 删除实体
	 * 
	 * @param guid
	 *            唯一值
	 */
	@DELETE
	@Path("/delete/{id:.+}")
	@Produces(javax.ws.rs.core.MediaType.APPLICATION_JSON)
	public void delete(@PathParam("id") String guid) {
		checkConfigService.delete(guid);
	}

	/**
	 * 指量删除实体
	 * 
	 * @param guid
	 *            唯一值集
	 */
	@DELETE
	@Path("/deletes/{ids:.+}")
	@Produces(javax.ws.rs.core.MediaType.APPLICATION_JSON)
	public void deletes(@PathParam("ids") String guids) {
		checkConfigService.deletes(guids);
	}
	
}
